package com.xuecheng.content.controller;

import com.xuecheng.api.content.model.file.UploadTokenResult;
import com.xuecheng.common.constant.ContentErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.RandomStringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName: FileManagerController
 * @Description: TODO
 * @author: 王亚军
 * @date: 2021/12/13  9:40
 */
@Slf4j
@RestController
public class FileManagerController {

    @Autowired
    private RestTemplate restTemplate;

    /*
   * #文件系统微服的请求地址
   file.service.url=http://127.0.0.1:56082/farming/generatetoken?origin=qiniu
   #文件存储空间名称
   file.service.bucket= xc136-static-imgs


   #文件存储区域的地址
   file.service.upload.region = http://upload.qiniu.com
   #文件访问的cdn加速域名
   cdn.domain = r3zc4s61r.hd-bkt.clouddn.com

   file.token.type = 1
   file.token.deadline = 3600
   * */
    @Value("${file.service.url}")
    private String url;
    @Value("${file.service.bucket}")
    private String bucket;
    @Value("${file.service.upload.region}")
    private String region;
    @Value("${cdn.domain}")
    private String domain;
    @Value("${file.token.type}")
    private String type;
    @Value("${file.token.deadline}")
    private int deadline;

    @GetMapping("/common/qnUploadToken")
    public UploadTokenResult uploadTokenResult() {


        //发送请求上传凭证

        HashMap<String, Object> map = new HashMap<>();

        map.put("scope", bucket);
        map.put("tokenType", type);
        map.put("deadline", deadline);

        //上传文件的key：唯一
        String fileKey = UUID.randomUUID().toString() + RandomStringUtils.randomAlphanumeric(16);

        map.put("key", fileKey);

        ResponseEntity<Map> responseEntity = restTemplate.postForEntity(url, map, Map.class);

        int codeValue = responseEntity.getStatusCodeValue();

        if (!(HttpStatus.OK.value() == codeValue)) {

            ExceptionCast.cast(ContentErrorCode.E_120022);
        }

        Map body = responseEntity.getBody();

        //2.将结果封装成前端所需要的内容并返回
        UploadTokenResult uploadTokenResult = new UploadTokenResult();

        uploadTokenResult.setTokenType(type);
        uploadTokenResult.setScope(bucket);
        uploadTokenResult.setKey(fileKey);
        uploadTokenResult.setQnToken(body.get("result").toString());
        uploadTokenResult.setUp_region(region);
        uploadTokenResult.setDomain(domain);
        uploadTokenResult.setDeadline(deadline);

        return uploadTokenResult;
    }
}
